<?php

namespace addons\fzdc\controller;

use app\common\controller\Api;
use app\admin\model\fzdc\Address as Ad;

class Address extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /*
     * 收货地址列表
     */
    public function list()
    {
        $user_id = $this->auth->id;
        $page = $this->request->post('page') ?? 1;  //页码
        $limit = $this->request->post('limit') ?? 10; //每页显示条数
        $data = Ad::where('user_id', $user_id)
            ->field("id,title,mobile,suffix,address,remark,tag,isDefault")
            ->paginate(["page" => $page, "list_rows" => $limit]);
        $result = array("total" => $data->total(), "rows" => $data->items());
        $this->success('返回成功', $result);
    }

    /*
     * 添加收货地址
     */
    public function add()
    {
        $user_id = $this->auth->id;
        $name = $this->request->post('name');         //姓名
        $suffix = $this->request->post('suffix');       //后缀
        $mobile = $this->request->post('mobile');       //手机号
        $address = $this->request->post('address');      //地址
        $lon = $this->request->post('lon');      //经度
        $lat = $this->request->post('lat');      //纬度
        $remark = $this->request->post('remark') ?? "";       //门牌号
        $tag = $this->request->post('tag');          //标签
        $isDefault = $this->request->post('isDefault') ?? 0; //是否默认地址1是 0否
        if (!$name) {
            $this->error("姓名不正确");
        }
        if (!$suffix) {
            $this->error("后缀不正确");
        }
        if (!$mobile) {
            $this->error("手机号不正确");
        }
        if (!$address) {
            $this->error("地址不正确");
        }
        if (!$tag) {
            $this->error("标签不正确");
        }
        if (!$lon) {
            $this->error("经度不正确");
        }
        if (!$lat) {
            $this->error("纬度不正确");
        }
        $res = Ad::create([
            "user_id" => $user_id,
            "title" => $name,
            "suffix" => $suffix,
            "mobile" => $mobile,
            "address" => $address,
            "remark" => $remark,
            "tag" => $tag,
            "lon" => $lon,
            "lat" => $lat,
            "isDefault" => $isDefault,
            "createtime" => time(),
            "updatetime" => time(),
        ]);
        if ($isDefault == 1) {
            Ad::where("id", "<>", $res->id)->where("user_id", "=", $user_id)->update(["isDefault" => 0]);
        }
        $this->success("成功");

    }

    /*
     * 删除收货地址
     */
    public function del()
    {
        $user_id = $this->auth->id;
        $address_id = $this->request->post('address_id');         //收货地址id
        //判断该用户是否有此收货地址
        $res = Ad::get(["user_id" => $user_id, "id" => $address_id]);
        if (!$res) {
            $this->error("该用户没有此收货地址");
        }
        Ad::destroy(["user_id" => $user_id, "id" => $address_id]);
        $this->success("成功");
    }

    /*
     * 修改收货地址
     */
    public function exec()
    {
        $user_id = $this->auth->id;
        $address_id = $this->request->post('address_id');   //收货地址id
        $name = $this->request->post('name');         //姓名
        $suffix = $this->request->post('suffix');       //后缀
        $mobile = $this->request->post('mobile');       //手机号
        $address = $this->request->post('address');      //地址
        $remark = $this->request->post('remark');       //门牌号
        $tag = $this->request->post('tag');          //标签
        $lon = $this->request->post('lon');      //经度
        $lat = $this->request->post('lat');      //纬度
        $isDefault = $this->request->post('isDefault') ?? 0; //是否默认地址1是 0否
        //判断该用户是否有此收货地址
        $res = Ad::get(["user_id" => $user_id, "id" => $address_id]);
        if (!$res) {
            $this->error("该用户没有此收货地址");
        }
        $res = Ad::where(["id" => $address_id, "user_id" => $user_id])->update([
            "title" => $name,
            "suffix" => $suffix,
            "mobile" => $mobile,
            "address" => $address,
            "remark" => $remark,
            "tag" => $tag,
            "lon" => $lon,
            "lat" => $lat,
            "isDefault" => $isDefault,
            "updatetime" => time(),
        ]);
        if ($isDefault == 1) {
            Ad::where("id", "<>", $address_id)->where("user_id", "=", $user_id)->update(["isDefault" => 0]);
        }
        $this->success("成功");

    }

}
